package com.action.tools;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.base.BaseAction;
import com.util.EncoderHandler;
import com.util.GUtil;

public class EncoderHandlerServlet extends BaseAction {

	private static final long serialVersionUID = -6419052154519296448L;

	private Logger log = Logger.getLogger(EncoderHandlerServlet.class);

	private JSONObject json;
	private PrintWriter out=null;

	@Override
	public String handle() throws Exception {
		log.info(this.getClass()+".handle()");

		//reflect 调用Action中的方法
		Method function = null;
		try{
			function = this.getClass().getDeclaredMethod(method);
		}catch(Exception e){
			function = null;
		}

		if(function!=null){
			function.invoke(this);
		}else{
			this.setMsg("ip=" + ip + "  "+this.getClass()+" cannot find the function @ "+function);
			log.info(msg);
		//	throw new Exception(msg);
		}
		
		return GUtil.NONE;
	}

	public String EncoderPasswordBase64Md5() throws IOException{

		json = new JSONObject();
		String password = request.getParameter("password");
		if(password==null || password.isEmpty()){
			log.error("function==null");
			msg = "password cannot be null or empty.";
			json.put("error", -1);
			json.put("msg",msg);
			out = response.getWriter();
			out.write(json.toString());
			out.close();
			return GUtil.NONE;
		}
		String str = EncoderHandler.EncoderPasswordBase64Md5(password);

		json.put("error", 0);
		json.put("msg", "OK");
		json.put("data", str);
		out = response.getWriter();
		out.write(json.toString());
		out.close();

		return GUtil.NONE;
	}


	public String help(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		out = resp.getWriter();
		out.write( this.getClass()+".hel()");
		out.close();
		return GUtil.NONE;
	}


	@Deprecated
	@Override
	public String query() throws Exception {
		return null;
	}


	@Deprecated
	@Override
	public String delete() throws Exception {
		return null;
	}

	@Deprecated
	@Override
	public String view() throws Exception {
		return null;
	}

	@Deprecated
	@Override
	public String add() throws Exception {
		return null;
	}

	@Deprecated
	@Override
	public String update() throws Exception {
		return null;
	}

	@Override
	public boolean validate(Object obj) throws Exception {
		return false;
	}


}
